package com.jeect.service.impl;

import com.jeect.domain.JwtUser;
import com.jeect.mapper.UserMapper;
import com.jeect.util.MyPasswordEncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Service
public class AuthenticationService implements UserDetailsService {
    @Autowired
    private MyPasswordEncode myPasswordEncode;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HttpServletRequest request;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("UserDetailsService............loadUserByUsername().");
        System.out.println("sessionid =========="+request.getSession().getId());
        com.jeect.base.domain.User user = null;
        if(!StringUtils.isEmpty(username)) {
            user = userMapper.getByAccount(username);
        }
        if(user==null){
            System.out.println("user is null");
            return null;
        }
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
        System.out.println(user.getLoginPass());
        return new User(username, user.getLoginPass(),//myPasswordEncode.encode("123456"),
                true,true,true,true,

                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

}
